package com.suql.networktools.net.presenter

import com.suql.networktools.bean.TResponse
import com.suql.networktools.bean.UserInfo
import com.suql.networktools.net.DataListener
import com.suql.networktools.net.RxHelper
import com.suql.networktools.net.contract.LoginContract
import com.suql.networktools.net.model.LoginModel

/**
 * @ClassName: LoginPresenter
 * @Dscription: 登陆Presenter
 * @Author: su qinglin
 * @Date: 2018/7/26 16:08
 */
class LoginPresenter(private val mView: LoginContract.IView) : LoginContract.IPresenter {

    private val mModel: LoginContract.IModel

    init {
        mModel = LoginModel()
    }

    override fun login(userName: String, password: String) {

        mModel.login(userName, password, object : DataListener<TResponse<UserInfo>> {
            override fun onResponse(result: TResponse<UserInfo>) {

                if (result.isSuccess) {

                    mView.loginSuccess(result.data, result.message)
                } else {

                    mView.loginFail(result.message)
                }
            }

            override fun onError(throwable: Throwable) {

                mView.loginFail(RxHelper.instance.getErrorInfo(throwable))
            }
        })
    }

    override fun unSubscribeTask() {

        mModel.unSubscribeTask()
    }
}
